package com.exartisan.thanhdoanhn;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.exartisan.util.DataHolder;
import com.exartisan.util.FbUtil;
import com.facebook.Session;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class HomeActivity extends SherlockFragmentActivity {
	ViewPager mViewPager;

	ActionBar.Tab Tab1, Tab2, Tab3;
	TabsAdapter mTabsAdapter;
	ActionBar actionBar;
	android.app.ActionBar mActionbar;
	int index;
	Integer images[] = { R.drawable.im_event, R.drawable.im_blog,
			R.drawable.im_gallary };
	Integer u_images[] = { R.drawable.unimg_event, R.drawable.unimg_blog,
			R.drawable.unimg_gallary };

	/*
	 * Fragment fragmentTab1 = new FragmentTab1(); Fragment fragmentTab2 = new
	 * FragmentTab2(); Fragment fragmentTab3 = new FragmentTab3();
	 */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mViewPager = new ViewPager(this);
		mViewPager.setId(R.layout.activity_home);
		setContentView(mViewPager);
		index = getIntent().getExtras().getInt("KEY");
		actionBar = getSupportActionBar();
		// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
		// getActionBar().hide();
		/*
		 * mActionbar=getActionBar(); mActionbar.show();
		 */

		// }
		Drawable myIcon = getResources().getDrawable(
				R.drawable.imv_header_background);
		actionBar.setStackedBackgroundDrawable(myIcon);
		// Hide Actionbar Icon
		actionBar.setDisplayShowHomeEnabled(false);
		// Hide Actionbar Title
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		mTabsAdapter = new TabsAdapter(this, mViewPager);

		/*
		 * ActionBar.Tab ac1=actionBar.newTab().setIcon(R.drawable.im_event);
		 * mTabsAdapter.addTab(ac1,FragmentBlog.class, null);
		 */
		mTabsAdapter.addTab(actionBar.newTab().setIcon(images[index]),
				FragmentEvent.class, null);
		mTabsAdapter.addTab(actionBar.newTab().setIcon(R.drawable.unimg_blog),
				FragmentBlog.class, null);
		mTabsAdapter
				.addTab(actionBar.newTab().setIcon(R.drawable.unimg_gallary),
						FragmentGallary.class, null);
		actionBar.setSelectedNavigationItem(index);

		if (savedInstanceState != null) {
			actionBar.setSelectedNavigationItem(savedInstanceState.getInt(
					"tab", 0));
		}

	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.logout :
			FbUtil.logout(getBaseContext());
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void onClickLike(View v) {
		FbUtil.likeId(DataHolder.FEED_ID_SAMPLE, this,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(int arg0, String response) {
						super.onSuccess(arg0, response);
					}

					@Override
					public void onFailure(Throwable arg0, String response) {
						super.onFailure(arg0, response);
					}

				});
	}

	public static class TabsAdapter extends FragmentPagerAdapter implements
			ViewPager.OnPageChangeListener,
			com.actionbarsherlock.app.ActionBar.TabListener {

		private final Context mContext;
		private final ActionBar mActionBar;
		private final ViewPager mViewPager;
		private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

		static final class TabInfo {
			private final Class<?> clss;
			private final Bundle args;

			TabInfo(Class<?> _class, Bundle _args) {
				clss = _class;
				args = _args;
			}
		}

		public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
			super(activity.getSupportFragmentManager());
			mContext = activity;
			mActionBar = activity.getSupportActionBar();
			mViewPager = pager;
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
		}

		public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
			TabInfo info = new TabInfo(clss, args);
			tab.setTag(info);
			tab.setTabListener(this);
			mTabs.add(info);
			mActionBar.addTab(tab);
			notifyDataSetChanged();
		}

		@Override
		public void onPageScrollStateChanged(int state) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
			mActionBar.setSelectedNavigationItem(position);
			Log.e("HomeActivity ", "position: " + position);

		}

		@Override
		public Fragment getItem(int position) {
			TabInfo info = mTabs.get(position);
			return Fragment.instantiate(mContext, info.clss.getName(),
					info.args);
		}

		@Override
		public int getCount() {
			return mTabs.size();
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			Object tag = tab.getTag();
			Log.e("Home", ": " + tag);
			for (int i = 0; i < mTabs.size(); i++) {
				if (mTabs.get(i) == tag) {
					mViewPager.setCurrentItem(i);

				}

			}

			switch (tab.getPosition()) {
			case 0:
				tab.setIcon(R.drawable.im_event);
				break;
			case 1:
				tab.setIcon(R.drawable.im_blog);

				// ft.add(R.id.fragment_content, mFragmentB, null);
				break;
			case 2:
				tab.setIcon(R.drawable.im_gallary);
				break;
			}

		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			switch (tab.getPosition()) {
			case 0:
				tab.setIcon(R.drawable.unimg_event);
				break;
			case 1:
				tab.setIcon(R.drawable.unimg_blog);

				// ft.add(R.id.fragment_content, mFragmentB, null);
				break;
			case 2:
				tab.setIcon(R.drawable.unimg_gallary);
				break;
			}

		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			// TODO Auto-generated method stub

		}

	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Session.getActiveSession().onActivityResult(this, requestCode,
				resultCode, data);

		/*switch (requestCode) {
		case 1:
			Session session = Session.getActiveSession();
			if (session != null) {
				session.onActivityResult(this, requestCode, resultCode, data);
			}
			break;
		default:
			Session.getActiveSession().onActivityResult(this, requestCode,
					resultCode, data);
			break;
		}*/

	}

}
